﻿using BaseUi.Device;
using BaseUi.Models;
using Newtonsoft.Json.Linq;
using Ping9719.DeviceCommunication;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BaseUi
{
    /// <summary>
    /// ShieldBoxView.xaml 的交互逻辑
    /// </summary>
    /// <summary>
    /// ShieldBoxView.xaml 的交互逻辑
    /// </summary>
    public partial class ShieldBoxView : UserControl
    {
        public ShieldBoxView()
        {
            InitializeComponent();
        }

        public CeShiBan DeviceData
        {
            get { return (CeShiBan)GetValue(DeviceDataProperty); }
            set { SetValue(DeviceDataProperty, value); }
        }

        public static readonly DependencyProperty DeviceDataProperty =
            DependencyProperty.Register("DeviceData", typeof(CeShiBan), typeof(ShieldBoxView), new PropertyMetadata(null));

        private void gx(object sender, RoutedEventArgs e)
        {
            try
            {
                var aaa = DeviceData.SendPackageReliable(new byte[] { });
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"成功\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        private void kx(object sender, RoutedEventArgs e)
        {
            try
            {
                var aaa = DeviceData.SendPackageReliable(new byte[] { });
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"成功\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        private void onoff(object sender, RoutedEventArgs e)
        {
            try
            {
                var aaa = DeviceData.ReadOnoff();
                if (aaa.IsSucceed)
                {
                    string retu="";
                    //textBoxInfo.AppendText($"成功\r\n");
                    //textBoxInfo.AppendText($"通信成功" + aaa.Value + "\r\n");
                    string[] msg = aaa.Value.Split(' ');
                    string get = $"GND测试值:{int.Parse(msg[0])}mV\r\n VDD测试值:{int.Parse(msg[1])}mV\r\n VCC测试值:{int.Parse(msg[2])}mV\r\n" +
                        $" LCDCLK测试值:{int.Parse(msg[3])}mV\r\n SW测试值:{int.Parse(msg[4])}mV\r\n LCDDAT测试值:{int.Parse(msg[5])}mV\r\n NON测试值:{int.Parse(msg[6])}mV\r\n" +
                        $" LCDSTB测试值:{int.Parse(msg[7])}mV\r\n";
                    if (int.Parse(msg[0]) > LocalFile.Config.UGND || int.Parse(msg[0]) < LocalFile.Config.DGND)
                    {
                        retu = retu+ "GND超出范围\r\n";
                    }
                    if (int.Parse(msg[1]) > LocalFile.Config.UVDD || int.Parse(msg[1]) < LocalFile.Config.DVDD)
                    {
                        retu = retu+ "VDD超出范围\r\n";
                    }
                    if (int.Parse(msg[2]) > LocalFile.Config.UVCC || int.Parse(msg[2]) < LocalFile.Config.DVCC)
                    {
                        retu = retu+ "VCC超出范围\r\n";
                    }
                    if (int.Parse(msg[3]) > LocalFile.Config.ULCDCLK || int.Parse(msg[3]) < LocalFile.Config.DLCDCLK)
                    {
                        retu = retu+ "LCDCLK超出范围\r\n";
                    }
                    if (int.Parse(msg[4]) > LocalFile.Config.USW || int.Parse(msg[4]) < LocalFile.Config.DSW)
                    {
                        retu = retu+ "SW超出范围\r\n";
                    }
                    if (int.Parse(msg[5]) > LocalFile.Config.ULCDDAT || int.Parse(msg[5]) < LocalFile.Config.DLCDDAT)
                    {
                        retu = retu+ "LCDDAT超出范围\r\n";
                    }
                    if (int.Parse(msg[6]) > LocalFile.Config.UNON || int.Parse(msg[6]) < LocalFile.Config.DNON)
                    {
                        retu = retu+ "NON超出范围\r\n";
                    }
                    if (int.Parse(msg[7]) > LocalFile.Config.ULCDSTB || int.Parse(msg[7]) < LocalFile.Config.DLCDSTB)
                    {
                        retu = retu+ "LCDSTB超出范围\r\n";
                    }
                    if (retu!="")
                    {
                        textBoxInfo.AppendText($"测试失败" + get + retu + "\r\n");
                    }
                    else
                    {
                        textBoxInfo.AppendText($"测试通过"+ get+"\r\n");
                    }
                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        private void Sensor(object sender, RoutedEventArgs e)
        {
            try
            {
                string retul = "";
                var aaa = DeviceData.ReadSensor();
                if (aaa.IsSucceed)
                {
                    int getnumber = int.Parse(aaa.Value);
                    if (getnumber < LocalFile.Config.DSensor[0])
                    {
                        retul = "有料且为按压状态";
                    }
                    else if (getnumber > LocalFile.Config.USensor[0])
                    {
                        retul = "无料";
                    }
                    else
                    {
                        retul = "有料但未按压到位";
                    }
                    textBoxInfo.AppendText($"状态：{retul}\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        private void Current(object sender, RoutedEventArgs e)
        {
            
            try
            {
                string retul = "";
                var aaa = DeviceData.ReadCurrent();
                if (aaa.IsSucceed)
                {
                    double getnumber = int.Parse(aaa.Value);
                    if (getnumber < LocalFile.Config.DUA)
                    {
                        retul = "电流小于下限";
                    }
                    else if (getnumber > LocalFile.Config.UUA)
                    {
                        retul = "电流大于上限";
                    }
                    else
                    {
                        retul = "电流在范围内";
                    }
                    textBoxInfo.AppendText($"电流：{((getnumber/100).ToString("F2"))}uA,状态：{retul}\r\n");

                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        private void Frequency(object sender, RoutedEventArgs e)
        {
           
            try
            {
                string retul = "";
                var aaa = DeviceData.ReadOnoff();
                aaa = DeviceData.ReadFrequency();
                if (aaa.IsSucceed)
                {
                    double getnumber = double.Parse(aaa.Value);
                    if (getnumber < LocalFile.Config.DHZ)
                    {
                        retul = "频率小于下限";
                    }
                    else if (getnumber > LocalFile.Config.UHZ)
                    {
                        retul = "频率大于上限";
                    }
                    else
                    {
                        retul = "频率在范围内";
                    }
                    textBoxInfo.AppendText($"频率：{((getnumber / 1000).ToString("F3"))}KHz,状态：{retul}\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        private void Currentck(object sender, RoutedEventArgs e)
        {
            
            try
            {
                var aaa = DeviceData.ReadCurrentcheck();
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"电流值：" + aaa.Value + "\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        private void Frequencyck(object sender, RoutedEventArgs e)
        {
            
            try
            {
                var aaa = DeviceData.ReadFrequencycheck();
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"频率" + aaa.Value + "\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        private void anonoff(object sender, RoutedEventArgs e)
        {
            string aavailu = "";
            //var aaa = LocalStatic.Electric.Readsentrelay(500, PortModel.Suppress, new List<int>(), LocalFile.Config.Address);
            //if (!aaa.IsSucceed)
            //{
            //    textBoxInfo.AppendText($"按压通信失败成功" + aaa.Value + "\r\n");
            //    return;
            //}
            //Thread.Sleep(200);
            ////aaa = LocalStatic.Electric.Readsentfrequency(500, PortModel.Pressing, LocalFile.Config.Address);
            //if (!MainWindow2.viewModel.ischengg)
            //{
            //    textBoxInfo.AppendText($"按压到位通信失败成功" + MainWindow2.viewModel.errtext + "\r\n");
            //    return;
            //}
            //aavailu = MainWindow2.viewModel.msg[10]+" "+MainWindow2.viewModel.msg[11]+" "+MainWindow2.viewModel.msg[12]+" "+MainWindow2.viewModel.msg[13]+" ";
            //if (aavailu!= "00 01 00 01 ")
            //{
            //    string[] strul = aavailu.Split(' ');
            //    if (strul[0]!= "00")
            //    {
            //        textBoxInfo.AppendText($"右按压气缸未按压" + "\r\n");
            //    }
            //    if (strul[1] != "01")
            //    {
            //        textBoxInfo.AppendText($"右按压气缸未按压到位" + "\r\n");
            //    }
            //    if (strul[2] != "00")
            //    {
            //        textBoxInfo.AppendText($"左按压气缸未按压" + "\r\n");
            //    }
            //    if (strul[3] != "01")
            //    {
            //        textBoxInfo.AppendText($"左按压气缸未按压到位" + "\r\n");
            //    }
            //    return;
            //}
            var aaa = DeviceData.ReadOnoff();
            if (aaa.IsSucceed)
            {
                string retu = "";
                string[] msg = aaa.Value.Split(' ');
                string get = $"\r\n按压通断GND值:{int.Parse(msg[0])}mV \r\n 按压通断SW值:{int.Parse(msg[4])}mV";
                if (int.Parse(msg[0]) > LocalFile.Config.buttUGND || int.Parse(msg[0]) < LocalFile.Config.buttDGND)
                {
                    retu = retu + "\r\n按压通断GND超出范围";
                }
                if (int.Parse(msg[4]) > LocalFile.Config.buttUSW || int.Parse(msg[4]) < LocalFile.Config.buttDSW)
                {
                    retu = retu + "\r\n按压通断SW超出范围";
                }
                if (retu != "")
                {
                    textBoxInfo.AppendText($"测试失败"   +get + retu + "\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"测试成功" +get + "\r\n");
                }

            }
           
            //aaa = LocalStatic.Electric.Readsentrelay(500, new List<int>(), PortModel.Suppress, LocalFile.Config.Address);
            //if (!aaa.IsSucceed)
            //{
            //    textBoxInfo.AppendText($"按压上升通信失败成功" + aaa.Value + "\r\n");
            //    return;
            //}
            //Thread.Sleep(200);
            //aaa = LocalStatic.Electric.Readsentfrequency(500, PortModel.Pressing, LocalFile.Config.Address);
            //aavailu = MainWindow2.viewModel.msg[10]+" "+MainWindow2.viewModel.msg[11]+" "+MainWindow2.viewModel.msg[12]+" "+MainWindow2.viewModel.msg[13]+" ";
            //if (!MainWindow2.viewModel.ischengg)
            //{
            //    textBoxInfo.AppendText($"按压到位通信失败成功" + MainWindow2.viewModel.errtext + "\r\n");
            //    return;
            //}
            //if (aavailu != "01 00 01 00 ")
            //{
            //    string[] strul = aavailu.Split(' ');
            //    if (strul[0] != "01")
            //    {
            //        textBoxInfo.AppendText($"1号按压未升到位" + "\r\n");
            //    }
            //    if (strul[1] != "00")
            //    {
            //        textBoxInfo.AppendText($"1号按压未升起" + "\r\n");
            //    }
            //    if (strul[2] != "01")
            //    {
            //        textBoxInfo.AppendText($"2号按压未升到位" + "\r\n");
            //    }
            //    if (strul[3] != "00")
            //    {
            //        textBoxInfo.AppendText($"2号按压未升起" + "\r\n");
            //    }
            //    return;
            //}

        }

        private void textchange(object sender, TextChangedEventArgs e)
        {
            var nam = (sender as TextBox);
            nam.ScrollToEnd();
        }

        private void clear(object sender, RoutedEventArgs e)
        {
            textBoxInfo.Clear();
        }
    }
}
